package com.cyandpos.pad.codedcoupon;


import com.cyandpos.pad.posdata.CodeCoupon;
import com.cyandpos.pad.posdata.CodeCouponPayLimit;
import com.cyandpos.pad.posdata.CodedCouponData;
import com.cyandpos.pad.posdata.QueryMemberRequest;
import com.cyandpos.pad.utils.MoneyUtils;
import com.cyandpos.pad.utils.log.MessageLog;

public class CodedCouponPresenter {
    private ICodedCouponView mView;
    private ICodedCouponModel mModel;

    public CodedCouponPresenter(ICodedCouponView view){
        this.mView = view;
        mModel = new CodedCouponModelImpl();
    }

    /**
     * 查询编码券列表
     * @param data
     */
    public void getCodedCouponList(QueryMemberRequest data) {
        mView.showWaitDialog("请稍后……");
        mModel.getCodedCouponList(data, new ICodedCouponModel.queryCodedCouponListener() {
            @Override
            public void getCodedCouponSuccess(CodedCouponData data, int flag) {//0 券号   1刷卡
                mView.dismissWaitDialog();
                //处理返回数据
                if(data.getCoupons() !=null && data.getCoupons().size()>0){
                    if(flag ==1){//刷卡查询
                        if(data.getCodeCouponPaylimit() != null && data.getCodeCouponPaylimit().size() >0 ){
                            CodedCouponData useData = new CodedCouponData();
                            useData.setVipId(data.getVipId());
                            useData.setVipCode(data.getVipCode());
                            for(CodeCoupon coupons : data.getCoupons()){
                                for(CodeCouponPayLimit limits :data.getCodeCouponPaylimit()){
                                    if(coupons.getCouponType() == limits.getCouponType()
                                            && coupons.getCodeCouponId()== limits.getCodeCouponId()){
                                        coupons.setVipId(useData.getVipId());
                                        coupons.setCodeCouponName(limits.getCodeCouponName());
                                        useData.getCoupons().add(coupons);
                                        break;
                                    }
                                }
                            }
                            if(useData.getCoupons().size()>0){
                                mView.getCodedCouponSuccess(useData);
                            }else{
                                mView.getCodedCouponFail("未获取到可用编码券");
                            }
                        }else{
                            mView.getCodedCouponFail("未获取到可用编码券");
                        }
                    }else{//券号查询
                        MessageLog.writeLog("通过卡券查询编码券的长度为："+ data.getCoupons().size());
                        if(data.getCoupons().size()>0 && MoneyUtils.getMoneyInt(data.getCoupons().get(0).getLimitMoney())>0){
                            mView.getCodedCouponSuccess(data);
                        }else{
                            mView.getCodedCouponFail("编码券可用金额为0！");
                        }

                    }

                }else{
                    mView.getCodedCouponFail("未获取到可用编码券");
                }
            }

            @Override
            public void getCodedCouponFailure(String msg) {
                mView.dismissWaitDialog();
                mView.getCodedCouponFail(msg);
            }
        });

    }

//    @Override
//    public void getDataFailure(String msg) {
//        mView.dismissWaitDialog();
//        mView.getCodedCouponFail(msg);
//    }

//    private Payment getYhqPayId(int yhqid) {
//        Payment payment = null;
//        for (Payment e : ErpCommonData.loadConfig.getSkfs()) {
//            if (e.getYhjid() == yhqid) {
//                payment = e;
//                break;
//            }
//        }
//        return payment;
//    }
}
